package org.xiaojl.modules.sys.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.xiaojl.modules.sys.entity.SysRoleResource;
import org.xiaojl.modules.sys.mapper.SysRoleResourceMapper;
import org.xiaojl.modules.sys.service.SysRoleResourceService;
import org.xiaojl.utils.ToolUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 角色资源 服务实现类
 * </p>
 *
 * @author xiaojl
 * @since 2017-08-18
 */
@Service
public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMapper, SysRoleResource> implements SysRoleResourceService {

	@Override
	public String selectResourceIdsByRoleId(Long roleId) {
		StringBuffer sb = new StringBuffer("");
		List<SysRoleResource> list = this.selectList(new EntityWrapper<SysRoleResource>().eq("roleId",roleId));
		for(SysRoleResource rr : list){
			if(ToolUtil.isNotEmpty(rr)){
				sb.append(rr.getResourceId());
				sb.append(",");
			}
		}
		return sb.toString();
	}

	@Override
	public void updateRoleResourceByRoleId(Long roleId, String selectVal) {
		//1 删除roleId对应的sys_role_resource
		this.delete(new EntityWrapper<SysRoleResource>().eq("roleId",roleId));
		
		//2新增
		if(ToolUtil.isNotEmpty(selectVal)){
			String[] strs = selectVal.split(",");
			if(strs!=null && strs.length>0){
				for(String str: strs){
					if(ToolUtil.isNotEmpty(str)){
						SysRoleResource rr = new SysRoleResource();
						rr.setRoleId(roleId);
						rr.setResourceId(Long.valueOf(str));
						this.insert(rr);
					}
				}
			}
		}
		
	}
	
}
